import Vue from 'vue'
import Vuex from 'vuex'
import { setToken, getToken, delToken } from '@/utils/storage'
import { authCurrentUser } from '@/api/user'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 因为刷新后会让存在vuex中的token数据消失，所以我们直接获取本地的token
    token: getToken(),
    userinfo: ''
  },
  getters: {},
  mutations: {
    setUserToken(state, val) {
      state.token = val
      // 将拿到的token存到本地
      setToken(val)
    },
    setUserMessage(state, res) {
      state.userinfo = res.data
    },
    removeToken(state) {
      state.token = null
      delToken()
    }
  },
  actions: {
    async getUserMessage(context) {
      const res = await authCurrentUser()
      console.log(res)
      context.commit('setUserMessage', res)
    }
  },
  modules: {}
})
